﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IService.Business;
using IService.DataDLL;
using IService.DataDLL.Utility;
using IService.Model;
using IService.Utility;

namespace IService.Admin.Pages
{
    public partial class ActivityApplyList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.BindData();
            }
        }

        public void BindData()
        {
            PagingEntity pe = new PagingEntity("ActivityApply", "ApplyDate desc");
            pe.PageSize = Pagination.PageSize;
            pe.PageIndex = Pagination.PageIndex;
            DataTable table = DTVBusiness.ActivityApplyManager.GetActivityApplyListPagin(pe);
            this.rptNewsList.DataSource = table;
            this.rptNewsList.DataBind();
            this.Pagination.BindName = "BindData";
            this.Pagination.Count = pe.RecordCount;
            this.Pagination.BindPager();
        }

        protected string GetUserNameById(string userId)
        {
            int parentid = int.Parse(userId);
            DTVAdmin user = DTVBusiness.AdminManager.GetUserById(parentid);
            if (user != null)
            {
                return user.Username;
            }
            return string.Empty;
        }

        protected string GetShortNewsContent(string content)
        {
            return StringUtil.GetShortContent(content, 40);
        }

        protected void DeleteNewsByLink(object sender, EventArgs e)
        {
            string Id = ((LinkButton)sender).CommandArgument.ToString();
            bool hassuccess = DTVBusiness.ActivityApplyManager.DeleteActivityApply(int.Parse(Id));
            if (hassuccess)
            {
                this.BindData();
                JsUtil.Alert("删除成功！", this.Page);
            }
            else
            {
                JsUtil.Alert("删除失败！", this.Page);
            }
        }
        
        protected void BenDeleteSelect(object sender, EventArgs e)
        {
            string ID = string.Empty;
            foreach (RepeaterItem item in this.rptNewsList.Items)
            {
                CheckBox CheckboxSign = item.FindControl("ChkSelect") as CheckBox;
                if (CheckboxSign.Checked == true)
                {
                    if (ID == string.Empty)
                    {
                        ID = ((HiddenField)this.rptNewsList.Items[item.ItemIndex].FindControl("HiddenId")).Value;
                    }
                    else
                    {
                        ID += "," + ((HiddenField)this.rptNewsList.Items[item.ItemIndex].FindControl("HiddenId")).Value;
                    }

                }
            }
            if (ID != string.Empty)
            {
                bool hassuccess = DTVBusiness.ActivityApplyManager.DeleteActivityApplyByIds(ID);
                if (hassuccess)
                {
                    this.BindData();
                    JsUtil.Alert("删除成功！", this.Page);
                }
                else
                {
                    JsUtil.Alert("删除失败！", this.Page);
                }
            }
            else
            {
                JsUtil.Alert("请选择需要删除的记录！", this.Page);
            }
        }

        protected void BtnSearchRecord_Click(object sender, EventArgs e)
        {
            string startDate = this.TxbStartDate.Text;
            string endDate = this.TxbEndDate.Text;
            startDate = DateTime.Parse(startDate).ToString("yyyy/MM/dd 00:00:00");
            endDate = DateTime.Parse(endDate).ToString("yyyy/MM/dd 23:59:59");
            PagingEntity pe = new PagingEntity("ActivityApply", "ApplyDate desc");
            pe.PageSize = Pagination.PageSize;
            pe.PageIndex = Pagination.PageIndex;
            pe.WhereClause = "(cdate(ApplyDate)>=cdate('" + startDate + "')) and (cdate(ApplyDate)<=cdate('" + endDate + "'))";
            DataTable table = DTVBusiness.ActivityApplyManager.GetActivityApplyListPagin(pe);
            this.rptNewsList.DataSource = table;
            this.rptNewsList.DataBind();
            this.Pagination.BindName = "BindData";
            this.Pagination.Count = pe.RecordCount;
            this.Pagination.BindPager();
        }

        //根据活动id获取活动名
        protected string GetActivityName(string id)
        {
            return GetActivityContent[id];
        }

        //活动集合
        protected Dictionary<string, string> GetActivityContent
        {
            get
            {
                Dictionary<string, string> result = ContentDetail;
                if (result == null)
                {
                    result = new Dictionary<string, string>();
                    DataTable table = DTVBusiness.ActivityManager.GetActivityListAll();
                    if (table != null)
                    {
                        foreach (DataRow dr in table.Rows)
                        {
                            result.Add(dr["ID"].ToString(), dr["Title"].ToString());                                                            
                        }
                    }
                    ContentDetail = result;
                }
                else
                {
                    //页面不存在
                }
                return result;
            }
        }

        protected Dictionary<string, string> ContentDetail
        {
            get
            {
                return (this.ViewState["contentDetail"] as Dictionary<string, string>);
            }
            set
            {
                this.ViewState["contentDetail"] = value;
            }
        }
    }
}